package com.fire.control.system.module.device.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fire.control.system.dto.CountDto;
import com.fire.control.system.dto.MapDeviceDTO;
import com.fire.control.system.dto.UserDTO;
import com.fire.control.system.dto.app.AppDeviceInfoVo;
import com.fire.control.system.dto.app.AppDeviceVo;
import com.fire.control.system.dto.app.AppFloorVo;
import com.fire.control.system.dto.app.AppMapDeviceVo;
import com.fire.control.system.dto.app.AppMixVo;
import com.fire.control.system.dto.app.AppWarningInfoVo;
import com.fire.control.system.entity.Company;
import com.fire.control.system.entity.Device;
import com.fire.control.system.module.device.dao.DeviceMapper;
import com.fire.control.system.module.device.service.AppDeviceService;
import com.fire.control.system.module.user.dao.UserMapper;
import com.fire.control.system.statics.Message_Response;
import com.fire.control.system.statics.ResponseMessage;
import com.fire.control.system.statics.Status_Online;
import com.fire.control.system.support.protocol.app.AppRequestProtocol;
import com.fire.control.system.support.protocol.comman.ProtocolSelector;
import com.fire.control.system.support.protocol.comman.ResponseProtocol;
import com.fire.control.system.support.protocol.device.DeviceRequestProtocol;
import com.fire.control.system.support.util.XdaoDate;
import com.github.pagehelper.StringUtil;

import antlr.StringUtils;

/**
 * 
 * @ClassName:  AppDeviceServiceImpl
 * @Description:  TODO
 * @author:  xiechengguang
 * @date:  2017年6月13日 下午6:16:19
 *
 */
@Service
public class AppDeviceServiceImpl implements AppDeviceService {
	@Autowired private ProtocolSelector protocolSelector;
	@Autowired private DeviceMapper deviceMapper;
	@Autowired private UserMapper userMapper;
	
	/**
	 * <p>Author: xiechengguang</p> 
	 * <p>Title: getDevices</p> 
	 * <p>Description: app端获取所有设备</p> 
	 * @param request
	 * @return 
	 * @see com.fire.control.system.module.device.service.AppDeviceService#getDevices(com.fire.control.system.support.protocol.app.AppRequestProtocol)
	 */
	@Override
	public ResponseProtocol getDevices(AppRequestProtocol request, UserDTO user) {
		ResponseProtocol response = protocolSelector.choose(request);
		
		List<AppDeviceVo> devices = deviceMapper.findByUserId(user.getUserId()); 
			
		if(devices != null && !devices.isEmpty()) {
			ResponseMessage.CODE_200.messageToProtocol(response);
			response.setDate(new XdaoDate());
			response.setData(devices);
			return response;
		}
		ResponseMessage.CODE_204.messageToProtocol(response);
		response.setDate(new XdaoDate());
		response.setData(null);
		return response;
	}
	
	/**
	 * <p>Author: xiechengguang</p> 
	 * <p>Title: getPlaneInfo</p> 
	 * <p>Description: 根据设备id获取设备平面图详细信息  </p> 
	 * @param request
	 * @return 
	 * @see com.fire.control.system.module.device.service.AppDeviceService#getPlaneInfo(com.fire.control.system.support.protocol.app.AppRequestProtocol)
	 */
	@Override
	public ResponseProtocol getPlaneInfo(AppRequestProtocol request) {
		ResponseProtocol response = protocolSelector.choose(request);
		
		String deviceId = request.getId();
		if(deviceId == null || StringUtil.isEmpty(deviceId)) {
			ResponseMessage.CODE_400.messageToProtocol(response);
			response.setDate(new XdaoDate());
			return response;
		}
		AppWarningInfoVo returnVo = deviceMapper.getByDeviceId(deviceId);  // deviceId命名不能更改
		ResponseMessage.CODE_200.messageToProtocol(response);
		response.setData(returnVo);
		response.setDate(new XdaoDate());
		return response;
	}
	
	/**
	 * <p>Author: xiechengguang</p> 
	 * <p>Title: getSideInfo</p> 
	 * <p>Description: 根据设备id获取设备侧面图详细信息</p> 
	 * @param request
	 * @return 
	 * @see com.fire.control.system.module.device.service.AppDeviceService#getSideInfo(com.fire.control.system.support.protocol.app.AppRequestProtocol)
	 */
	@Override
	public ResponseProtocol getSideInfo(AppRequestProtocol request) {
		ResponseProtocol response = protocolSelector.choose(request);
		
		String deviceId = request.getId();
		if(deviceId == null || StringUtil.isEmpty(deviceId)) {
			ResponseMessage.CODE_400.messageToProtocol(response);
			response.setDate(new XdaoDate());
			return response;
		}
			
		AppDeviceInfoVo returnVo = deviceMapper.findByDeviceId(deviceId); // deviceId命名不能更改
		Integer floor = returnVo.getFloor();
		String groupId = returnVo.getGroupId();
		
		int count = deviceMapper.countThisFloor(groupId, floor);  // 当前楼层设备总数
		int normal = deviceMapper.normalThisFloor(groupId, floor); // 当前楼层正常设备数
		int alarm = deviceMapper.alarmThisFloor(groupId, floor);  // 当前楼层报警设备数
		int fault = deviceMapper.faultThisFloor(groupId, floor);  // 当前楼层故障设备数
		returnVo.setCount(count);
		returnVo.setAlarm(alarm);
		returnVo.setFault(fault);
		returnVo.setNormal(normal);
		
		ResponseMessage.CODE_200.messageToProtocol(response);
		response.setData(returnVo);
		response.setDate(new XdaoDate());
		return response;
	}
	
	
	/**
	 * @author: xiechengguang 
	 * @Title: getAllBuildingDevices   
	 * @Description: 根据当前设备获取楼栋所有设备，统计每层  
	 * @param:        
	 * @return: ResponseProtocol      
	 * @throws
	 */
	@Override
	public ResponseProtocol getAllBuildingDevices(AppRequestProtocol requestProtocol, UserDTO user) {
		ResponseProtocol response = protocolSelector.choose(requestProtocol);
		if(requestProtocol != null) {
			String id = requestProtocol.getId();
			String companyId = user.getCompany().getId();
			// 获取所在设备组id
			AppMixVo mix = deviceMapper.getGroupIdAndFloormaxByDeviceId(id);
			//获取所在楼栋的每层设备数统计
			List<AppFloorVo> countList = deviceMapper.getAppAllFloorsById(id, mix.getGroupId()); 
			Map<String, Object> map = new HashMap<>();
			
			if(countList != null && !countList.isEmpty()) {
				for(AppFloorVo c : countList) {
					Integer floor = c.getFloor();
					if(floor != null) {
						// 获取每层的设备列表
						List<AppMapDeviceVo> deviceList = deviceMapper.eachAppFloorDevices(floor, mix.getGroupId());
						c.setList(deviceList);
					}
				}
				map.put("floorList", countList);
				map.put("floorMax", mix.getFloorMax());
				ResponseMessage.CODE_200.messageToProtocol(response);
				response.setData(map);
				return response;
			}
			ResponseMessage.CODE_204.messageToProtocol(response);
			return response;
		}
		ResponseMessage.CODE_400.messageToProtocol(response);
		return response;
	}
	
	

}
